package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.Admin;
import com.pojo.Role;
import com.service.AdminService;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 验证管理员登录
     * @param account
     * @param password
     * @return
     */
    @RequestMapping("/selectAdminByAccountAndPassword")
    public Object selectAdminByAccountAndPassword(String account,String password){
        R r=new R<Role>();
        Admin admin = adminService.selectAdminByAccountAndPassword(account, password);
        if(admin!=null){
            r.setCode(200);
            r.setData(admin);
        }else{
            r.setCode(400);
            r.setMessage("该管理员不存在");
        }
        return r;
    }

    /**
     * 根据id查询管理员信息
     * @param id
     * @return
     */
    @RequestMapping("/selectAdminById")
    public Object selectAdminById(long id){
        R r=new R<Role>();
        Admin admin = adminService.selectAdminById(id);
        if(admin!=null){
            r.setCode(200);
            r.setData(admin);
        }else{
            r.setCode(400);
            r.setMessage("该管理员不存在");
        }
        return r;
    }

    /**
     *name模糊查询 分页查询
     * @param pageNum
     * @param account
     * @return
     */
    @RequestMapping("/selectAdminByAccount")
    public Object selectAdminByAccount(@RequestParam(value = "pageNum",required = true,defaultValue= "1") int pageNum, @RequestParam(value = "account",required = true,defaultValue= "") String account  ){
        R r=new R<Role>();
        Map map=new HashMap();
        map.put("pageNum",pageNum);
        map.put("account",account);
        PageInfo pageInfo = adminService.selectAdminByAccount(map);
        r.setCode(200);
        r.setData(pageInfo);
        return r;
    }

    /**
     * 更新管理员信息
     * @param admin
     * @return
     */
    @RequestMapping("/updateAdmin")
    public Object updateAdmin(Admin admin){
        R r=new R<Role>();
        int i = adminService.updateAdmin(admin);
        if(i!=0){
            r.setCode(200);
            r.setMessage("更新成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("更新失败");
            r.setData(0);
        }
        return r;
    }


    /**
     * 插入管理员
     * @param admin
     * @return
     */
    @RequestMapping("/insertAdmin")
    public Object insertAdmin(Admin admin){
        R r=new R<Role>();
        int i = adminService.insertAdmin(admin);
        if(i!=0){
            r.setCode(200);
            r.setMessage("插入成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("插入失败");
            r.setData(0);
        }
        return r;
    }

    /**
     * 根据id删除管理员
     * @param id
     * @return
     */
    @RequestMapping("/deleteAdminById")
    public Object deleteAdminById(long id){
        R r=new R<Role>();
        int i = adminService.deleteAdminById(id);
        if(i!=0){
            r.setCode(200);
            r.setMessage("删除成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("删除失败");
            r.setData(0);
        }
        return r;
    }


    /**
     * 根据id恢复管理员
     * @param id
     * @return
     */
    @RequestMapping("/resumeAdminById")
    public Object resumeAdminById(long id){
        R r=new R<Role>();
        int i = adminService.resumeAdminById(id);
        if(i!=0){
            r.setCode(200);
            r.setMessage("恢复成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("恢复失败");
            r.setData(0);
        }
        return r;
    }

    /**
     * 角色分配
     * @param id 管理员id
     * @param roleid 角色id
     * @return
     */
    @RequestMapping("/insertAdminRoles")
    public Object insertAdminRoles(long id,long[] roleid){
        R r=new R<Role>();
        int i = adminService.insertAdminRoles(id,roleid);
        if(i!=0){
            r.setCode(200);
            r.setMessage("分配成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("分配失败");
            r.setData(0);
        }
        return r;
    }
}
